//
//  TabView.swift
//  onemeetingmac
//
//  Created by 张衡 on 2019/12/17.
//  Copyright © 2019 onemeeting. All rights reserved.
//

import Foundation

@objc protocol TabViewDelegate {
    @objc func tabView_mouseDown(event: NSEvent,senber:TabView)
    @objc func tabView_rightButtomAction(tab:TabView)
}

enum TabType {
    case `default`
    case onlyIcon
    case custom
}

struct TabGroup {
    var image:String?
    var title:String?
    var custom:NSView?
    var cusSize:CGSize = CGSize.init(width: 30, height: 30)
    init(image:String?,title:String?) {
        self.image = image
        self.title = title
    }
}

class TabView: MSView {
    
    var kImageW = CGFloat(30)
    var kImageH = CGFloat(30)
    
    var list:TabGroup!
    
    weak var delegate:TabViewDelegate?
    
    open var hover:Bool = false {
        didSet {
            let color = hover ? color_gray(120).cgColor:NSColor.clear.cgColor
            self.hoverBg.layer?.backgroundColor = color
           
        }
    }
    
    var hasRight:Bool = false {
        didSet {
            self.rightButtom.isHidden = !hasRight
        }
    }
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
    }
    
    convenience init(type:TabType = .default,list:TabGroup) {
        self.init()
        self.list = list
        switch type {
        case .onlyIcon:
            self.setupIcon()
        case .custom:
            self.setupCustom()
        default:
            self.setupUI()
        }
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupIcon()  {
        
        self.addSubview(hoverBg)
        hoverBg.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(5, 0, 5, 0))
        }
        
        self.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.width.equalTo(kImageW)
            make.height.equalTo(kImageH)
            make.centerY.equalToSuperview()
            make.centerX.equalToSuperview()
        }
        self.icon.image = NSImage.init(named: NSImage.Name.init(self.list.image!))
    }
    
    
    func setupCustom()  {
        
        self.addSubview(hoverBg)
        hoverBg.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(5, 0, 5, 0))
        }
        
        self.addSubview(custom)
        custom.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.top.equalToSuperview().offset(10)
            make.bottom.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
        }
        
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.bottom.equalToSuperview().offset(-3)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(rightButtom)
        rightButtom.snp.makeConstraints { (make) in
            make.right.equalTo(-3)
            make.top.equalToSuperview().offset(8)
            make.width.height.equalTo(20)
        }
        
        self.title.stringValue = self.list.title!
        
        self.custom.addSubview(self.list.custom!)
        self.list.custom?.snp.makeConstraints({ (make) in
            make.size.equalTo(self.list.cusSize)
            make.center.equalToSuperview()
        })
    }
   
    
    func setupUI()  {
        
        self.addSubview(hoverBg)
        hoverBg.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(NSEdgeInsetsMake(5, 0, 5, 0))
        }
        
        self.addSubview(icon)
        icon.snp.makeConstraints { (make) in
            make.width.equalTo(kImageW)
            make.height.equalTo(kImageH)
            make.top.equalToSuperview().offset(8)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(title)
        title.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalTo(20)
            make.bottom.equalToSuperview().offset(-3)
            make.centerX.equalToSuperview()
        }
        self.addSubview(rightButtom)
        rightButtom.snp.makeConstraints { (make) in
            make.right.equalTo(-3)
            make.top.equalToSuperview().offset(8)
            make.width.height.equalTo(20)
        }
        
        self.icon.image = NSImage.init(named: NSImage.Name.init(self.list.image!))
        self.title.stringValue = self.list.title ?? ""
        
    }
    
    lazy var rightButtom: ZHButton = {
        let btn = ZHButton.init()
        btn.normalImage = NSImage.image(name:"ic_pullup") ?? NSImage.init()
        btn.hasBorder = true
        btn.zh_cornerRadius = 6
        btn.backgroundNormalColor = NSColor.clear
        btn.backgroundSelectedColor = color_gray(180)
        btn.backgroundHoverColor = color_gray(180)
        btn.backgroundHighlightColor = color_gray(180)
        btn.backgroundViewColor = NSColor.clear
        btn.target = self
        btn.action = #selector(rightButtomAction(sender:))
        btn.isHidden = true
        return btn
    }()
    
    lazy var hoverBg: NSView = {
        let view = NSView.init()
        view.wantsLayer = true
        view.layer?.cornerRadius = 10
        view.layer?.backgroundColor = NSColor.clear.cgColor
        return view
    }()
    
    lazy var custom: NSView = {
        let view = NSView()
        return view
    }()
    
    lazy var icon: NSImageView = {
        let imageV = NSImageView.init(image: NSImage.init(named: NSImage.Name.init("toolbar_stop_video_normal"))!)
        return imageV
    }()
    
    lazy var title: NSLabel = {
        let tef = NSLabel.label(title: "", textColor: color_gray(220), font: NSFont.systemFont(ofSize: 12))
        tef.alignment = .center
        return tef
    }()
    
    @objc dynamic func tap(_ senber:NSView) {
        
    }
    
    @objc func rightButtomAction(sender:ZHButton) {
        sender.backgroundViewColor = color_gray(255,alpha: 0.3)
        self.delegate?.tabView_rightButtomAction(tab:self)
    }
    
}
//MARK: - 鼠标触发事件
extension TabView {
    
    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        self.hover = true
    }
    
    
    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        self.hover = false
    }
    
    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        self.hover = true
        self.delegate?.tabView_mouseDown(event:event, senber: self)
    }
    
    override func cursorUpdate(with event: NSEvent) {
         NSCursor.openHand.set()
    }
    
}
